package com.yueya.common.city;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.render.JsonRender;
import com.yueya.common.action.BaseController;
import com.yueya.common.city.model.City;
import com.yueya.common.model.Json;
import com.yueya.common.model.Tree;

/**
 * 地区管理
 * 
 * @author wuenyang
 *
 */
public class CityController extends BaseController {
	Map<String, String> map = new HashMap<String, String>();

	public void index() {
		render("/WEB-INF/views/basic/city/city_list.jsp");
	}

	@SuppressWarnings("rawtypes")
	public void getAllListPager() {
		map.clear();
		map.put("pid", this.getPara("pid"));
		map.put("name", this.getPara("name"));
		Page page = City.service.pagination(this.getPage(), this.getRows(), map);
		jsonMap.put("total", page.getTotalRow());
		jsonMap.put("rows", page.getList());
		renderJson(JsonKit.toJson(jsonMap));

	}

	/**
	 * Tree list
	 */
	public void listTree() {
		int pid = this.getParaToInt(0);
		List<Tree> list = City.service.getListByParentId(pid);
		renderJson(JsonKit.toJson(list));
	}

	/**
	 * To Edit View
	 */
	public void doEdit() {
		int id = this.getParaToInt("id");
		City city = City.dao.findById(id);
		this.setAttr("city", city);
		render("/WEB-INF/views/basic/city/city_edit.jsp");
	}

	public void doAdd() {
		this.setAttr("pcity", City.dao.findById(this.getParaToInt("pid")));
		render("/WEB-INF/views/basic/city/city_add.jsp");
	}

	@SuppressWarnings("rawtypes")
	public void getList() {
		int level = this.getParaToInt(0, 1);
		List list = City.service.getListByLevel(level);
		renderJson(list);
	}

	@Before(Tx.class)
	public void save() {
		boolean bl = true;

		City city = this.getModel(City.class);
		if (StrKit.notNull(city.get("id"))) {
			bl = city.update();
		} else {
			int ccityid = (City.service.getMaxId() + 1);
			city.set("id", ccityid);
			if (city.getInt("parent_id") != 0)
				city.set("domain", "|" + ccityid);
			else
				city.set("domain", City.dao.findById(city.get("parent_id")).getStr("domain"));
			bl = city.save();
		}

		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("操作成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("操作失败!");
		}
		this.renderJson(j);

	}

	/**
	 * 操作：删除
	 */
	public void doDelete() {
		int id = this.getParaToInt("id");
		City city = City.dao.findById(id);
		boolean bl = false;
		bl = city.delete();
		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

	public void doDeletes() {
		Boolean bl = false;
		String idsStr = this.getPara("ids");
		if (idsStr != null) {
			String[] ids = idsStr.split(";");
			City city = null;
			for (int i = 0; i < ids.length; i++) {

				if (!"".equals(ids[i]) && ids[i] != null) {
					city = City.dao.findById(ids[i]);

					if (city.delete()) {
						bl = true;
					} else {
						bl = false;
						break;
					}
				}

			}
		}

		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

	@Clear
	public void domain() {
		City.service.domain("0");
		renderJson("结束");
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void getListCity() {
		String name = this.getPara("name");
		int level = this.getParaToInt("level");
		List<City> list = new ArrayList();
		if (StrKit.notBlank(name)) {
			list = City.service.getListByName(name, level);
		}
		renderJson(list);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void getRegine() {
		String name = this.getPara("name");
		int level = this.getParaToInt("level");
		List<Record> list = new ArrayList();
		if (StrKit.notBlank(name)) {
			list = City.service.getListByNameRecord(name, level);
		}
		renderJson(list);
	}

	public void list() {
		String id = getPara(0, "0");
		renderJson(new JsonRender(City.dao.getListByParentId(id)));
	}

}
